import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
// import Swiper from 'vue-awesome-swiper';
import Swiper from 'vue-awesome-swiper';
import 'swiper/dist/css/swiper.min.css';
import App from './App.vue';
import router from './router';
import * as IsToken from './api/isToken.js'//验证token是否存在
import cookie from './api/cookie.js'//当前cookie
import echarts from 'echarts'

Vue.prototype.$echarts = echarts 

Vue.use(ElementUI);
Vue.use(Swiper);
//权限指令
Vue.directive('has', {
  bind: function(el, binding) {
    if (!Vue.prototype.$_has(binding.value)) {
      el.parentNode.removeChild(el);
    }
  }
});
//路由拦截,判断token是否存在
router.beforeEach(function (to, from, next) {
  window.scrollTo(0, 0);//scroll置顶
  /*if (IsToken.isExistToken()) {
    IsToken.getIsToken((res) => {
      if (res.retCode == 1) {
        next();
      } else {
        cookie.removeCookie("merchant_token");//删除过期登录信息
        router.push({path: '/login'});
      }
    })
  } else {
    if (to.path == '/login' || to.path=='/register' || to.path=='/' || to.path=='' || to.path=='transaction') {
      next();
    } else {
      router.push({path: '/login'});
    }
  }*/
  if (IsToken.isExistToken()) {
    /*IsToken.getIsToken.r().then((res) => {
    
      if (res.retCode == 1) {
        next();
      } else {
        cookie.removeCookie("merchant_token");//删除过期登录信息
        router.push({path: '/login'});
      }
    })*/
    let routerArr=router.options.routes;
    for(var i=0;i<routerArr.length;i++){
      if(to.path==routerArr[i].path || ("/"+to.path.split("/")[1])==routerArr[i].path){
         sessionStorage.setItem('activeIndex',i+'');

      }
    } 
    next();
  } else {
    if (to.path == '/login' || to.path == '/' || to.path=='/register' || to.path=='/transaction' || to.path=='/forgetpassword' ) {
      next();
    } else {
      router.push({path: '/login'});
    }
  }
/*  if(sessionStorage.getItem("islogin")=="login") { 
    next();
  } else {
 
    if (to.path == '/login' || to.path == '/' || to.path=='/register' || to.path=='/transaction' || to.path=='/personusercenter') {
      next();
    } else {
      router.push({path: '/login'});
    }
  }*/
});
new Vue({
  el: '#app',
  router,
  render: h => h(App)
});

